package com.example.home.controller;

import com.example.home.Dao.*;
import com.example.home.Entities.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("students")
public class StudentController {

    @Autowired
    StudentRepository studentRepository;

    @Autowired
    TeacherRepository teacherRepository;

    @Autowired
    CollageRepository collageRepository;

    @Autowired
    CateRepository cateRepository;

    @Autowired
    CourseRepository courseRepository;

    @Autowired
    MajorsRepository  majorsRepository;

    @GetMapping
    public String outStudentForms(Model model){
        List<Student> students = studentRepository.findAll();
        model.addAttribute("student",students);
        return "students/list-Student";

    }


    @GetMapping("/new")
    public String outStudentForm(Model model){

        Student student = new Student();
        model.addAttribute("student",student);

        List<Teacher> teacher0 = teacherRepository.findAll();
        List<Collage> collage1 = collageRepository.findAll();
        List<Cate> cate1 = cateRepository.findAll();
        List<Course> course1 = courseRepository.findAll();
        List<Majors> major1 = majorsRepository.findAll();

        model.addAttribute("tea",teacher0);
        model.addAttribute("collage0",collage1);
        model.addAttribute("cate0",cate1);
        model.addAttribute("course0",course1);
        model.addAttribute("major0",major1);


        return "students/Student";
    }

    @PostMapping("save")
    public String CreateStudent(Model model, Student student){

        //处理保存数据库的行为
        studentRepository.save(student);


        //使用定位防止重复提交
        return "redirect:/students/new";

    }

}
